import router from '@/router'
import store from '@/store'
import NProgress from 'nprogress'
import 'nprogress/nprogress.css'

// 前置路由守卫
const whiteList = ['/login', '/404'] // 定义白名单  所有不受权限控制的页面
router.beforeEach(async(to, from, next) => {
  NProgress.start()
  const token = store.getters.token
  console.log(token)
  if (token) {
    if (to.path === '/login') {
      next('/')
    } else {
      // const res = await store.dispatch('user/getUserInfo')
      // console.log('未来做权限控制需要用到的数据', res)
      // 避免重复发送请求先做判断
      if (!store.state.user.userInfo.userId) {
        const res = await store.dispatch('user/getUserInfo')
        console.log('未来做权限控制需要用到的数据', res)
      }
      next()
    }
  } else {
    if (whiteList.includes(to.path)) {
      next()
    } else {
      next('/login')
    }
  }
})

// 后置守卫
router.afterEach((to, from) => {
  NProgress.done()
})
